//
//  YWMaterialAddOrSubTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/5/26.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWMaterialAddOrSubTableViewCell.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWMaterialStock.h"
#import "YWMaterial.h"
#import "YWCommonTool.h"
#import "YWConst.h"
#import "YWMacro.h"
#import "YWMaterialStockOutDetail.h"
#import "YWMaterialReturnDetail.h"

@interface YWMaterialAddOrSubTableViewCell(){
    NSInteger firstSelectCount;
    NSInteger stockId;
}
/*物料名称*/
@property (nonatomic, strong) UILabel * materialLabel;
/*减少数量*/
@property (nonatomic, strong) UIButton * cutButton;
/*增加数量*/
@property (nonatomic, strong) UIButton * addButton;
/*显示、输入数量*/
@property (nonatomic, strong) UITextField * numbleTextField;
@end

@implementation YWMaterialAddOrSubTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

//- (void)setStock:(YWMaterialStock *)stock{
//    _stock = stock;
//    firstSelectCount = _stock.selectQuantity;
//    _numbleTextField.tag = 100+stock.stockId;
//    _materialLabel.text = stock.materialInfo.materialName;
//    _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_stock.selectQuantity];
//}

- (void)setStockOutDetail:(YWMaterialStockOutDetail *)stockOutDetail{
    _stockOutDetail = stockOutDetail;
    firstSelectCount = _stockOutDetail.quantity;
    stockId = stockOutDetail.detailId;
    _numbleTextField.tag = 100+_stockOutDetail.detailId;
    _materialLabel.text = _stockOutDetail.materialStock.materialInfo.materialName;
    _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_stockOutDetail.quantity];
}

- (void)setReturnDetail:(YWMaterialReturnDetail *)returnDetail{
    _returnDetail = returnDetail;
    firstSelectCount = _returnDetail.quantity;
    stockId = _returnDetail.detailId;
    _numbleTextField.tag = 100+_returnDetail.detailId;
    _materialLabel.text = _returnDetail.materialStock.materialInfo.materialName;
    _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_returnDetail.quantity];
}

- (void)setUpSubviews{
    [self.materialLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(6);
        make.left.mas_equalTo(self.contentView).offset(8);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.bottom.mas_equalTo(self.contentView).offset(-6);
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.materialLabel.mas_centerY);
        make.right.mas_equalTo(self.contentView).offset(-8);
        make.size.mas_equalTo(CGSizeMake(23, 23));
    }];
    [self.numbleTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.materialLabel.mas_centerY);
        make.right.mas_equalTo(self.addButton.mas_left).offset(-8);
        make.size.mas_equalTo(CGSizeMake(40, 23));
    }];
    [self.cutButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.materialLabel.mas_centerY);
        make.right.mas_equalTo(self.numbleTextField.mas_left).offset(-8);
        make.size.mas_equalTo(CGSizeMake(23, 23));
    }];
}

- (UILabel*)materialLabel{
    if (!_materialLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _materialLabel = label;
        [self.contentView addSubview:_materialLabel];
    }
    return _materialLabel;
}

- (UIButton*)cutButton{
    if (!_cutButton) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"common_delete"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(cutButtonNumberEvent:) forControlEvents:UIControlEventTouchUpInside];
        _cutButton = button;
        [self.contentView addSubview:_cutButton];
    }
    return _cutButton;
}

- (UITextField*)numbleTextField{
    if (!_numbleTextField) {
        UITextField * textField = [[UITextField alloc] init];
        [textField addTarget:self action:@selector(textFieldEditChange:) forControlEvents:UIControlEventEditingChanged];
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.textAlignment = NSTextAlignmentCenter;
        textField.clipsToBounds = YES;
        textField.layer.cornerRadius = 3.0;
        textField.layer.borderWidth = 1.0;
        textField.layer.borderColor = [UIColor customTextGrayColor].CGColor;
        _numbleTextField = textField;
        [self.contentView addSubview:_numbleTextField];
    }
    return _numbleTextField;
}

- (UIButton*)addButton{
    if (!_addButton) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"common_add"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addButtonNumberEvent:) forControlEvents:UIControlEventTouchUpInside];
        _addButton = button;
        [self.contentView addSubview:_addButton];
    }
    return _addButton;
}

- (void)textFieldEditChange:(id)sender{
    if (stockId == _numbleTextField.tag-100) {
        if (_numbleTextField.text.length > 4){
            [YWCommonTool showTextHUDWithContent:@"输入数量不能超过10000"];
            NSRange rangeRange = [_numbleTextField.text rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 4)];
            _numbleTextField.text = [_numbleTextField.text substringWithRange:rangeRange];
        }
        
        NSString *content = _numbleTextField.text;
        NSInteger count = [content integerValue];
        if (count>firstSelectCount) {
            [YWCommonTool showTextHUDWithContent:@"添加数量不能大于申请数量"];
        }else{
            if (_stockOutDetail) {
                _stockOutDetail.quantity = count;
            }else{
                _returnDetail.quantity = count;
            }
            _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long) count];
        }
    }
}

- (void)cutButtonNumberEvent:(UIButton*)sender{
    if (_stockOutDetail) {
        if (_stockOutDetail.quantity > 0) {
            _stockOutDetail.quantity = _stockOutDetail.quantity -1;
            _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long) _stockOutDetail.quantity];
        }else{
            _stockOutDetail.quantity = 0;
        }
    }else{
        if (_returnDetail.quantity > 0) {
            _returnDetail.quantity = _returnDetail.quantity -1;
            _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long) _returnDetail.quantity];
        }else{
            _returnDetail.quantity = 0;
        }
    }
}

- (void)addButtonNumberEvent:(UIButton*)sender{
    if (_stockOutDetail) {
        if (_stockOutDetail.quantity>firstSelectCount) {
            [YWCommonTool showTextHUDWithContent:@"添加数量不能大于申请数量"];
        }else{
            _stockOutDetail.quantity = _stockOutDetail.quantity + 1;
            _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_stockOutDetail.quantity];
        }
    }else{
        if (_returnDetail.quantity>firstSelectCount) {
            [YWCommonTool showTextHUDWithContent:@"添加数量不能大于领用数量"];
        }else{
            _returnDetail.quantity = _returnDetail.quantity + 1;
            _numbleTextField.text = [NSString stringWithFormat:@"%ld",(long)_returnDetail.quantity];
        }
    }
}

@end
